{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import torch"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "# how to perform random sampling of the tensors"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<torch._C.Generator at 0x1069bd310>"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.manual_seed(1234)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[-0.1117, -0.4966,  0.1631, -0.8817],\n",
       "        [ 0.0539,  0.6684, -0.0597, -0.4675],\n",
       "        [-0.2153,  0.8840, -0.7584, -0.3689],\n",
       "        [-0.3424, -1.4020,  0.3206, -1.0219]])"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.manual_seed(1234)\n",
    "torch.randn(4,4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "#generate random numbers from a statistical distribution"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[0.2837, 0.6567, 0.2388, 0.7313],\n",
       "        [0.6012, 0.3043, 0.2548, 0.6294],\n",
       "        [0.9665, 0.7399, 0.4517, 0.4757],\n",
       "        [0.7842, 0.1525, 0.6662, 0.3343]])"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.Tensor(4, 4).uniform_(0, 1) #random number from uniform distribution"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "#now apply the distribution assuming the input values from the \n",
    "#tensor are probabilities"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[1., 1., 1., 1.],\n",
       "        [0., 0., 0., 0.],\n",
       "        [0., 0., 1., 0.],\n",
       "        [1., 1., 0., 0.]])"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.bernoulli(torch.Tensor(4, 4).uniform_(0, 1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "#how to perform sampling from a multinomial distribution"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([10., 10., 13., 10., 34., 45., 65., 67., 87., 89., 87., 34.])"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.Tensor([10, 10, 13, 10,34,45,65,67,87,89,87,34])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([ 8,  7, 11])"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.multinomial(torch.tensor([10., 10., 13., 10., \n",
    "                                34., 45., 65., 67., \n",
    "                                87., 89., 87., 34.]), \n",
    "                  3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([ 5, 10,  5,  9, 10])"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.multinomial(torch.tensor([10., 10., 13., 10., \n",
    "                                34., 45., 65., 67., \n",
    "                                87., 89., 87., 34.]), \n",
    "                  5, replacement=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "#generate random numbers from the normal distribution"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([1.2111, 2.3034, 3.5310, 4.7278, 6.1060, 6.3294, 6.9060, 7.9908, 9.3492,\n",
       "        9.9928])"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.normal(mean=torch.arange(1., 11.), \n",
    "             std=torch.arange(1, 0, -0.1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([-1.1794, -2.9019,  2.4459,  7.5613,  5.9058])"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.normal(mean=0.5, \n",
    "             std=torch.arange(1., 6.))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([0.7487])"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.normal(mean=0.5, \n",
    "             std=torch.arange(0.2,0.6))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor(45.9167)"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#computing the descriptive statistics: mean\n",
    "torch.mean(torch.tensor([10., 10., 13., 10., 34., \n",
    "                         45., 65., 67., 87., 89., 87., 34.]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[-0.2632, -0.5432, -1.6406,  0.9295, -1.2777],\n",
       "        [-0.7428,  0.9711,  0.3551,  0.8562, -0.3635],\n",
       "        [-0.1552, -1.2282, -0.8039, -0.4530, -0.2217],\n",
       "        [-2.0901, -1.2658, -1.8761, -0.6066,  0.7470]])"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# mean across rows and across columns\n",
    "d = torch.randn(4, 5)\n",
    "d"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([-0.8128, -0.5165, -0.9914,  0.1815, -0.2789])"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.mean(d,dim=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([-0.5590,  0.2152, -0.5724, -1.0183])"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.mean(d,dim=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(tensor([-0.7428, -1.2282, -1.6406, -0.4530, -0.3635]),\n",
       " tensor([1, 2, 0, 2, 1]))"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#compute median\n",
    "torch.median(d,dim=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(tensor([-0.5432,  0.3551, -0.4530, -1.2658]), tensor([1, 2, 3, 1]))"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.median(d,dim=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(tensor([-1.6406, -0.7428, -1.2282, -2.0901]), tensor([2, 0, 1, 0]))"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# compute the mode\n",
    "torch.mode(d)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(tensor([-2.0901, -1.2658, -1.8761, -0.6066, -1.2777]),\n",
       " tensor([3, 3, 3, 3, 0]))"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.mode(d,dim=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(tensor([-1.6406, -0.7428, -1.2282, -2.0901]), tensor([2, 0, 1, 0]))"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.mode(d,dim=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor(0.9237)"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#compute the standard deviation\n",
    "torch.std(d)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([0.8890, 1.0459, 1.0087, 0.8243, 0.8287])"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.std(d,dim=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([0.9987, 0.7507, 0.4458, 1.1436])"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.std(d,dim=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor(0.8532)"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#compute variance\n",
    "torch.var(d)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([0.7903, 1.0939, 1.0175, 0.6795, 0.6868])"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.var(d,dim=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([0.9974, 0.5636, 0.1987, 1.3079])"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.var(d,dim=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor(-2.0901)"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# compute min and max\n",
    "torch.min(d)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(tensor([-2.0901, -1.2658, -1.8761, -0.6066, -1.2777]),\n",
       " tensor([3, 3, 3, 3, 0]))"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.min(d,dim=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(tensor([-1.6406, -0.7428, -1.2282, -2.0901]), tensor([2, 0, 1, 0]))"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.min(d,dim=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor(0.9711)"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.max(d)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(tensor([-0.1552,  0.9711,  0.3551,  0.9295,  0.7470]),\n",
       " tensor([2, 1, 1, 0, 3]))"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.max(d,dim=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(tensor([ 0.9295,  0.9711, -0.1552,  0.7470]), tensor([3, 1, 0, 4]))"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.max(d,dim=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(tensor([[-1.6406, -1.2777, -0.5432, -0.2632,  0.9295],\n",
       "         [-0.7428, -0.3635,  0.3551,  0.8562,  0.9711],\n",
       "         [-1.2282, -0.8039, -0.4530, -0.2217, -0.1552],\n",
       "         [-2.0901, -1.8761, -1.2658, -0.6066,  0.7470]]),\n",
       " tensor([[2, 4, 1, 0, 3],\n",
       "         [0, 4, 2, 3, 1],\n",
       "         [1, 2, 3, 4, 0],\n",
       "         [0, 2, 1, 3, 4]]))"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# sorting a tensor\n",
    "torch.sort(d)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(tensor([[-2.0901, -1.2658, -1.8761, -0.6066, -1.2777],\n",
       "         [-0.7428, -1.2282, -1.6406, -0.4530, -0.3635],\n",
       "         [-0.2632, -0.5432, -0.8039,  0.8562, -0.2217],\n",
       "         [-0.1552,  0.9711,  0.3551,  0.9295,  0.7470]]),\n",
       " tensor([[3, 3, 3, 3, 0],\n",
       "         [1, 2, 0, 2, 1],\n",
       "         [0, 0, 2, 1, 2],\n",
       "         [2, 1, 1, 0, 3]]))"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.sort(d,dim=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(tensor([[-0.1552,  0.9711,  0.3551,  0.9295,  0.7470],\n",
       "         [-0.2632, -0.5432, -0.8039,  0.8562, -0.2217],\n",
       "         [-0.7428, -1.2282, -1.6406, -0.4530, -0.3635],\n",
       "         [-2.0901, -1.2658, -1.8761, -0.6066, -1.2777]]),\n",
       " tensor([[2, 1, 1, 0, 3],\n",
       "         [0, 0, 2, 1, 2],\n",
       "         [1, 2, 0, 2, 1],\n",
       "         [3, 3, 3, 3, 0]]))"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.sort(d,dim=0,descending=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(tensor([[ 0.9295, -0.2632, -0.5432, -1.2777, -1.6406],\n",
       "         [ 0.9711,  0.8562,  0.3551, -0.3635, -0.7428],\n",
       "         [-0.1552, -0.2217, -0.4530, -0.8039, -1.2282],\n",
       "         [ 0.7470, -0.6066, -1.2658, -1.8761, -2.0901]]),\n",
       " tensor([[3, 0, 1, 4, 2],\n",
       "         [1, 3, 2, 4, 0],\n",
       "         [0, 4, 3, 2, 1],\n",
       "         [4, 3, 1, 2, 0]]))"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.sort(d,dim=1,descending=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [],
   "source": [
    "from torch.autograd import Variable"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[1., 1.],\n",
       "        [1., 1.]], requires_grad=True)"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Variable(torch.ones(2,2),requires_grad=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [],
   "source": [
    "a, b = 12,23\n",
    "x1 = Variable(torch.randn(a,b),\n",
    "            requires_grad=True)\n",
    "x2 = Variable(torch.randn(a,b),\n",
    "            requires_grad=True)\n",
    "x3 =Variable(torch.randn(a,b),\n",
    "            requires_grad=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor(3303.0298, grad_fn=<SumBackward0>)\n"
     ]
    }
   ],
   "source": [
    "c = x1 * x2\n",
    "d = a + x3\n",
    "e = torch.sum(d)\n",
    "\n",
    "e.backward()\n",
    "\n",
    "print(e)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[ 0.3474,  0.2856, -0.0495,  0.6225,  0.1657,  0.3158,  0.2492, -0.4978,\n",
       "         -0.0170, -0.2830, -0.6446, -0.0199,  1.6222,  1.4239,  0.9069,  0.0763,\n",
       "          0.3422, -0.2873, -0.6726,  0.8843, -0.6659, -0.7335, -0.2760],\n",
       "        [ 0.5549, -0.0609,  2.1073, -0.9930,  1.4080,  0.2597,  0.8076, -1.2618,\n",
       "         -0.7711,  1.3940,  0.6060,  0.2209, -0.8245, -0.0256, -1.9850, -0.3788,\n",
       "         -1.5030, -0.0175, -0.9194, -0.9389, -1.9421,  0.9805, -0.0745],\n",
       "        [-0.7118,  1.6136, -0.9792,  0.8285, -0.2501,  0.1602,  0.2078,  1.1879,\n",
       "         -0.7320,  1.3668,  0.9069,  1.5691, -0.1108, -0.2573,  0.6365, -0.4852,\n",
       "         -0.7144, -0.0600,  0.1858, -0.1704,  1.0270,  0.0704,  0.4298],\n",
       "        [ 0.8205, -1.4562,  0.3013, -0.2252,  0.1193, -1.0772,  0.2516,  0.0669,\n",
       "          1.4607,  0.7712,  0.0012,  0.3775,  0.7332, -0.4915, -1.1049,  0.1970,\n",
       "         -0.1435,  1.4660, -0.1042, -1.0545, -0.1973,  0.8522, -0.4066],\n",
       "        [-1.2096,  0.0365,  0.8257, -0.6476, -0.5563, -0.6508, -0.2179,  0.7700,\n",
       "         -0.9156,  0.7128,  1.1037, -0.7119,  1.5389,  0.2984,  0.5561, -0.6868,\n",
       "         -0.9922, -0.2442, -0.3475, -0.4015,  0.3828,  1.2363, -1.0535],\n",
       "        [-0.9404, -0.2963, -2.3561,  0.0111, -0.5872,  0.7376, -1.3158, -0.4562,\n",
       "         -1.7884, -0.9547,  0.3856,  1.2921,  0.8001, -1.1206, -1.6680,  1.0685,\n",
       "         -0.1831, -1.0058,  0.3797, -0.2985, -1.3616,  0.2083, -0.7378],\n",
       "        [-0.3218, -0.9491,  0.3462, -1.2652, -2.1208,  1.5305, -1.2153, -0.0316,\n",
       "         -0.8687, -0.3510, -1.5684,  1.3443,  0.0372, -0.3598, -0.9728, -1.2452,\n",
       "         -0.0115, -1.0545, -2.1433, -0.5419, -0.4255,  1.8125, -1.6073],\n",
       "        [-1.1039, -1.8680,  0.8119,  0.9280,  2.5353,  0.7432, -0.9266, -0.5365,\n",
       "         -0.2293, -0.2484,  0.3544, -0.1016, -0.3634,  1.1053, -0.9109,  0.6526,\n",
       "         -1.3451,  0.9636,  0.8350, -1.2857, -0.2141,  0.1037, -0.4039],\n",
       "        [ 2.1130, -0.0856,  1.2580, -1.8494, -0.9156,  1.0041,  0.4068, -0.6712,\n",
       "          0.4296, -1.3418, -0.3649,  1.2697, -0.6731, -0.5749,  0.9241,  0.7276,\n",
       "          0.8789, -0.0990,  1.2043,  0.1998, -0.8210, -0.8354, -0.3332],\n",
       "        [-1.3532,  2.7640, -0.3187, -0.1316, -0.6221,  1.3070,  1.4547, -0.2104,\n",
       "         -0.3415,  1.2042, -0.3535,  0.3415,  1.3207, -1.0124, -0.6572, -0.5746,\n",
       "         -2.0788, -1.0132, -0.3668,  1.3064, -2.1880, -1.0477,  0.3120],\n",
       "        [-0.8272, -0.8594,  0.6629,  0.5436,  0.5328, -0.7291, -1.1725,  0.0918,\n",
       "         -0.5656, -1.1900, -1.5155, -1.2364, -0.6758, -0.0800,  0.3137, -0.7110,\n",
       "          0.0634, -3.0735, -0.1539,  1.4019, -0.0312,  1.4725,  1.2173],\n",
       "        [ 0.6089, -1.1243,  0.7627, -0.6287, -0.7702,  1.6713,  0.7765,  1.2884,\n",
       "         -0.3930, -0.2439, -0.3857, -0.0959,  0.9456, -1.2708,  0.2959,  0.3936,\n",
       "         -0.0626,  0.5728, -0.2525, -0.0732, -0.0150,  0.1451,  0.2020]])"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x1.data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[-0.5864, -0.0455, -0.7560, -1.1689, -0.9389,  0.8857,  1.3764, -0.7828,\n",
       "          0.1678,  0.2797, -0.6915, -0.5131,  1.1427, -1.0126,  0.1160, -1.0350,\n",
       "         -0.6508, -0.0779, -0.0912,  0.5024, -0.4517, -0.4807,  0.5907],\n",
       "        [ 0.1634, -0.2600,  1.6992, -2.1036, -1.8971,  1.4661,  0.6120,  0.7994,\n",
       "          0.9301,  0.2647, -0.4921,  0.3055,  0.5843, -2.1502,  0.4151,  0.0032,\n",
       "         -0.3858, -0.7181, -1.8785, -0.2401, -1.3253,  1.1415, -0.4416],\n",
       "        [ 0.3492, -1.6730,  0.5750,  0.3497,  1.1152, -0.5058,  0.1733, -0.6813,\n",
       "          0.0256,  1.2838, -0.3343, -1.3312,  1.3023, -1.4379, -1.7115, -0.8005,\n",
       "          1.4921, -0.1077, -0.0295, -0.2446, -0.5917,  0.0851,  0.0752],\n",
       "        [ 0.8398,  1.3866, -0.9521,  0.5690, -0.3689,  1.3491,  1.5738,  1.2403,\n",
       "          0.8706, -0.7415,  1.7150,  0.9138,  1.1533,  0.5870,  1.7935, -0.4375,\n",
       "         -0.4464, -0.7372,  0.0616,  0.2363,  0.0920, -0.1961, -0.5081],\n",
       "        [-0.8078,  1.6439, -1.2366,  0.0595, -0.1920,  1.0108, -1.6116,  1.0756,\n",
       "         -0.0522,  1.0433, -0.1780,  0.5411, -1.3052,  0.3292, -1.0489,  0.5993,\n",
       "         -1.1722, -0.3579,  2.7906, -0.1816, -0.1384,  1.4555,  1.2022],\n",
       "        [-0.4833, -0.2876,  1.0301,  0.7295,  0.1031,  0.2971, -0.7516,  0.5605,\n",
       "          0.8605, -1.0222, -1.1600,  0.7207,  0.1496, -0.6501,  1.9048, -1.8408,\n",
       "          0.9628,  0.0223, -1.4754,  0.4335, -0.2378,  1.6771, -0.1949],\n",
       "        [ 0.3164,  1.4717, -0.3906,  0.0974,  1.0617,  1.2896,  0.6305,  0.9396,\n",
       "         -1.1102,  0.0076, -1.0987, -0.5351, -0.0020,  1.0430, -0.1040,  0.9702,\n",
       "         -0.7979,  0.9474,  0.5919, -0.4824,  0.0896,  0.5009,  0.9495],\n",
       "        [-1.1080, -1.1848, -1.4867, -1.1119, -0.5569, -0.1938,  0.6627,  0.6738,\n",
       "          0.1218,  1.5987,  0.4411,  1.3639,  0.3180, -0.1626, -0.0417, -0.1020,\n",
       "          1.6282,  2.1635, -0.6939, -0.6784, -1.4126, -0.6112, -0.2759],\n",
       "        [ 1.0002,  1.4745,  1.1294,  1.4856, -0.6763,  0.0937, -0.0664, -1.0708,\n",
       "          1.4148, -1.7715,  0.7818, -0.6357, -0.3934,  0.8038,  0.5010, -0.6928,\n",
       "          0.2405, -0.6921,  0.5878,  1.2261,  1.1008, -0.3873,  0.3288],\n",
       "        [ 0.6457, -1.4167, -0.2295, -0.8685,  0.0774,  1.1841,  0.5054, -0.5692,\n",
       "          1.1186,  0.5155,  1.4136, -2.0170, -1.3915,  1.3255,  0.4364, -1.7325,\n",
       "          0.4229, -0.2223, -1.8954,  1.4466, -1.2209,  0.4006,  0.3069],\n",
       "        [ 0.5484,  2.1183,  0.1040, -0.6036,  0.2447, -0.2365,  1.1019, -1.5382,\n",
       "          0.0870, -0.6785,  0.8507, -0.6447,  0.7930,  0.3650, -0.7848, -0.3514,\n",
       "          0.5550, -0.2747,  0.4913, -0.6194, -0.8629, -0.2413, -1.2446],\n",
       "        [-2.1552,  0.6139,  0.8651, -2.9441, -1.1591, -0.3071,  0.2001, -0.9961,\n",
       "         -1.6909,  0.3310,  0.6250, -0.7701,  1.0212,  0.7180, -0.1095,  0.0732,\n",
       "         -0.4882, -0.1455,  0.9776,  0.3895, -0.6950, -0.0835, -0.0569]])"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x2.data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[ 0.4955, -0.7306,  0.2499,  0.2458,  0.1029, -0.6494, -0.3258,  0.8149,\n",
       "          0.5885,  1.6232,  0.4841,  0.3293, -1.2471,  0.2117,  1.4315,  0.0502,\n",
       "          0.9736,  0.0087, -0.7901,  0.0267, -0.3120,  2.4534,  0.7926],\n",
       "        [ 0.2382,  1.8277,  0.6758, -2.0323, -0.3952,  0.7286,  1.3579,  1.3583,\n",
       "         -0.5818, -1.3091,  1.5569, -1.4629,  0.6109,  0.2401, -0.5455,  2.9154,\n",
       "         -0.1985, -0.2369,  0.1182, -0.8322,  0.1364,  0.4531, -0.1112],\n",
       "        [ 1.0307, -1.1862,  0.3823,  0.0338,  0.9814, -1.9638, -0.2218,  0.1477,\n",
       "         -0.1875,  0.9963, -1.9271,  0.8275,  1.7371, -0.7392, -0.1709,  1.2120,\n",
       "          0.9385,  0.3245, -1.6697, -0.5777, -1.4787, -0.2400,  0.4558],\n",
       "        [-1.0911,  0.1566, -1.0629,  0.5193,  0.4776, -0.7194,  0.3499, -0.3935,\n",
       "         -0.3622, -3.4058, -0.1108,  1.2183,  0.2079, -0.0961, -0.6697, -0.1985,\n",
       "         -1.1560, -0.7709, -0.4655,  1.6369, -0.6030, -0.7429, -0.5210],\n",
       "        [-0.8078, -0.4448,  1.4265,  0.2566,  0.9585,  0.1260, -0.5296, -0.7132,\n",
       "         -0.1676, -0.6071,  0.0347, -0.9721, -0.0397, -0.8161,  0.3938, -0.7728,\n",
       "         -0.6024, -0.3242, -0.6425, -2.3893,  0.4362, -2.6706,  0.4027],\n",
       "        [-1.0844,  0.1360,  2.1896, -0.2159, -0.4342,  1.4058,  0.5348, -0.1235,\n",
       "          0.6418, -2.1429, -0.0135,  0.9174, -1.8431, -0.7970, -0.1183, -1.1231,\n",
       "         -0.6695,  0.0306, -2.3209,  1.9433, -1.7311, -0.2967, -0.9412],\n",
       "        [ 1.6822,  1.3038, -0.3663, -1.2401, -0.2566, -0.0977,  0.5266,  1.5978,\n",
       "         -0.7700, -0.1483, -0.8830, -1.5841, -0.3711, -1.2338,  0.9990,  0.7565,\n",
       "          0.0178,  0.3047,  0.8214, -2.6705,  0.3366, -0.4533, -0.2432],\n",
       "        [ 1.2577,  0.7919, -0.8666, -0.3528, -1.5244, -0.4039, -1.4359,  1.1314,\n",
       "         -1.6357, -1.2673, -0.1306, -0.3743, -1.1143, -1.0104,  0.5946,  0.2325,\n",
       "          0.2863, -0.4498,  0.0428,  0.2114,  0.3053,  0.1590,  0.6634],\n",
       "        [ 0.2989,  0.4927,  1.2331,  0.0465, -0.0451,  0.6444,  1.6787,  0.8955,\n",
       "          0.4696, -1.1288,  1.4870, -1.2376, -0.0362, -0.2950, -2.5790, -1.0069,\n",
       "         -0.7403,  1.8117,  0.2949,  0.0973, -0.1098,  1.5281, -0.4748],\n",
       "        [-0.5837,  0.1302,  0.6672, -0.2173,  0.4118, -1.8131, -0.7694, -0.7774,\n",
       "         -0.4924,  2.7923,  0.3186, -0.2974, -0.6724,  0.8060,  0.1783, -0.8092,\n",
       "          0.2455,  1.0400,  1.5255, -0.2207,  1.3099, -1.3060, -1.4669],\n",
       "        [-0.3317, -0.4190, -0.6929, -0.3424,  2.0673, -1.0484,  0.4922,  1.7666,\n",
       "          0.0404,  0.4461,  0.1478,  0.6247, -0.7872,  0.7080, -0.4652,  0.5982,\n",
       "          0.2778,  1.7961,  1.4173,  1.0742, -0.8698,  0.4279,  1.7435],\n",
       "        [-0.2529, -1.1322, -0.5567,  0.2062,  0.1834,  1.2011, -0.7812,  0.5411,\n",
       "          0.8900, -0.2705, -3.0171,  0.0981, -0.1275,  0.1092, -0.2081, -0.7164,\n",
       "          0.5565, -0.5517, -0.1958, -0.7510,  1.3651,  0.3416,  1.2432]])"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x3.data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 102,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Gradient of w1 w.r.t to Loss: -455.0\n",
      "Gradient of w2 w.r.t to Loss: -365.0\n",
      "Gradient of w3 w.r.t to Loss: -60.0\n",
      "Gradient of w4 w.r.t to Loss: -265.0\n"
     ]
    }
   ],
   "source": [
    "from torch import FloatTensor\n",
    "from torch.autograd import Variable\n",
    "\n",
    "a = Variable(FloatTensor([5]))\n",
    "\n",
    "weights = [Variable(FloatTensor([i]), requires_grad=True) for i in (12, 53, 91, 73)]\n",
    "\n",
    "w1, w2, w3, w4 = weights\n",
    "\n",
    "b = w1 * a\n",
    "c = w2 * a\n",
    "d = w3 * b + w4 * c\n",
    "Loss = (10 - d)\n",
    "\n",
    "Loss.backward()\n",
    "\n",
    "for index, weight in enumerate(weights, start=1):\n",
    "    gradient, *_ = weight.grad.data\n",
    "    print(f\"Gradient of w{index} w.r.t to Loss: {gradient}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Using forward pass\n",
    "def forward(x):\n",
    "    return x * w"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "predict (before training) 4 tensor(4.)\n"
     ]
    }
   ],
   "source": [
    "import torch\n",
    "from torch.autograd import Variable\n",
    "\n",
    "x_data = [11.0, 22.0, 33.0]\n",
    "y_data = [21.0, 14.0, 64.0]\n",
    "\n",
    "w = Variable(torch.Tensor([1.0]),  requires_grad=True)  # Any random value\n",
    "\n",
    "# Before training\n",
    "print(\"predict (before training)\",  4, forward(4).data[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [],
   "source": [
    "# define the Loss function\n",
    "def loss(x, y):\n",
    "    y_pred = forward(x)\n",
    "    return (y_pred - y) * (y_pred - y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\tgrad:  11.0 21.0 tensor(-220.)\n",
      "\tgrad:  22.0 14.0 tensor(2481.6001)\n",
      "\tgrad:  33.0 64.0 tensor(-51303.6484)\n",
      "progress: 0 tensor(604238.8125)\n",
      "\tgrad:  11.0 21.0 tensor(118461.7578)\n",
      "\tgrad:  22.0 14.0 tensor(-671630.6875)\n",
      "\tgrad:  33.0 64.0 tensor(13114108.)\n",
      "progress: 1 tensor(39481139200.)\n",
      "\tgrad:  11.0 21.0 tensor(-30279010.)\n",
      "\tgrad:  22.0 14.0 tensor(171986000.)\n",
      "\tgrad:  33.0 64.0 tensor(-3358889472.)\n",
      "progress: 2 tensor(2590022582665216.)\n",
      "\tgrad:  11.0 21.0 tensor(7755301376.)\n",
      "\tgrad:  22.0 14.0 tensor(-44050112512.)\n",
      "\tgrad:  33.0 64.0 tensor(860298674176.)\n",
      "progress: 3 tensor(169906757784039325696.)\n",
      "\tgrad:  11.0 21.0 tensor(-1986333900800.)\n",
      "\tgrad:  22.0 14.0 tensor(11282376818688.)\n",
      "\tgrad:  33.0 64.0 tensor(-220344807849984.)\n",
      "progress: 4 tensor(11145967797036089329319936.)\n",
      "\tgrad:  11.0 21.0 tensor(508751660449792.)\n",
      "\tgrad:  22.0 14.0 tensor(-2889709562888192.)\n",
      "\tgrad:  33.0 64.0 tensor(56436029183229952.)\n",
      "progress: 5 tensor(731181229735636676902291243008.)\n",
      "\tgrad:  11.0 21.0 tensor(-130304505987203072.)\n",
      "\tgrad:  22.0 14.0 tensor(740129586448171008.)\n",
      "\tgrad:  33.0 64.0 tensor(-14454731813580439552.)\n",
      "progress: 6 tensor(47965853635691301104421115938209792.)\n",
      "\tgrad:  11.0 21.0 tensor(33374367042303426560.)\n",
      "\tgrad:  22.0 14.0 tensor(-189566395124581138432.)\n",
      "\tgrad:  33.0 64.0 tensor(3702231642950980337664.)\n",
      "progress: 7 tensor(inf)\n",
      "\tgrad:  11.0 21.0 tensor(-8548040947181920714752.)\n",
      "\tgrad:  22.0 14.0 tensor(48552870733517462437888.)\n",
      "\tgrad:  33.0 64.0 tensor(-948237585286470398115840.)\n",
      "progress: 8 tensor(inf)\n",
      "\tgrad:  11.0 21.0 tensor(2189375167048686422196224.)\n",
      "\tgrad:  22.0 14.0 tensor(-12435651156362409707307008.)\n",
      "\tgrad:  33.0 64.0 tensor(242868246054469617554817024.)\n",
      "progress: 9 tensor(inf)\n"
     ]
    }
   ],
   "source": [
    "# Run the Training loop\n",
    "for epoch in range(10):\n",
    "    for x_val, y_val in zip(x_data, y_data):\n",
    "        l = loss(x_val, y_val)\n",
    "        l.backward()\n",
    "        print(\"\\tgrad: \", x_val, y_val, w.grad.data[0])\n",
    "        w.data = w.data - 0.01 * w.grad.data\n",
    "\n",
    "        # Manually set the gradients to zero after updating weights\n",
    "        w.grad.data.zero_()\n",
    "\n",
    "    print(\"progress:\", epoch, l.data[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "predict (after training) 4 tensor(-9268691075357861748932608.)\n"
     ]
    }
   ],
   "source": [
    "# After training\n",
    "print(\"predict (after training)\",  4, forward(4).data[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[-0.7270,  4.2119, -0.3071, -3.6691],\n",
      "        [-2.8417, -1.1818, -1.4654, -0.4344],\n",
      "        [-2.0130, -2.3842,  1.3962,  1.4962],\n",
      "        [-2.0996,  1.8881, -1.9797,  0.2337]])\n"
     ]
    }
   ],
   "source": [
    "z = Variable(torch.Tensor(4, 4).uniform_(-5, 5))\n",
    "print(z)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Requires Gradient : False \n",
      "Volatile : False \n",
      "Gradient : None \n",
      "tensor([[-0.7270,  4.2119, -0.3071, -3.6691],\n",
      "        [-2.8417, -1.1818, -1.4654, -0.4344],\n",
      "        [-2.0130, -2.3842,  1.3962,  1.4962],\n",
      "        [-2.0996,  1.8881, -1.9797,  0.2337]])\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Applications/anaconda3/lib/python3.6/site-packages/ipykernel_launcher.py:2: UserWarning: volatile was removed (Variable.volatile is always False)\n",
      "  \n"
     ]
    }
   ],
   "source": [
    "print('Requires Gradient : %s ' % (z.requires_grad))\n",
    "print('Volatile : %s ' % (z.volatile))\n",
    "print('Gradient : %s ' % (z.grad))\n",
    "print(z.data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 83,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "torch.Size([4, 4])\n"
     ]
    }
   ],
   "source": [
    "x = Variable(torch.Tensor(4, 4).uniform_(-4, 5))\n",
    "y = Variable(torch.Tensor(4, 4).uniform_(-3, 2))\n",
    "# matrix multiplication\n",
    "z = torch.mm(x, y)\n",
    "print(z.size())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 84,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[-2.4582, -3.9389,  2.6662,  0.5629],\n",
       "        [ 4.2173, -1.1122, -3.8421, -1.9672],\n",
       "        [-2.5722, -0.3963, -3.4676,  2.3133],\n",
       "        [ 3.7414,  0.6285, -3.9795,  2.0648]])"
      ]
     },
     "execution_count": 84,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x.data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 103,
   "metadata": {},
   "outputs": [],
   "source": [
    "#tensor operations"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 109,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[0.7748, 0.5287, 0.8794, 0.0527],\n",
       "        [0.2824, 0.0036, 0.4439, 0.1425],\n",
       "        [0.7889, 0.8024, 0.2917, 0.9638],\n",
       "        [0.1534, 0.1216, 0.4023, 0.6097]])"
      ]
     },
     "execution_count": 109,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "mat1 = torch.FloatTensor(4,4).uniform_(0,1)\n",
    "mat1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 110,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[0.9493, 0.1600, 0.8915, 0.7011],\n",
       "        [0.2557, 0.1726, 0.6665, 0.4602],\n",
       "        [0.9003, 0.5600, 0.0388, 0.3431],\n",
       "        [0.3064, 0.2131, 0.4333, 0.7120],\n",
       "        [0.1883, 0.4072, 0.2834, 0.8132]])"
      ]
     },
     "execution_count": 110,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "mat2 = torch.FloatTensor(5,4).uniform_(0,1)\n",
    "mat2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 111,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([0.8854, 0.9122, 0.9897, 0.2618])"
      ]
     },
     "execution_count": 111,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "vec1 = torch.FloatTensor(4).uniform_(0,1)\n",
    "vec1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 112,
   "metadata": {},
   "outputs": [],
   "source": [
    "# scalar addition"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 113,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[11.2748, 11.0287, 11.3794, 10.5527],\n",
       "        [10.7824, 10.5036, 10.9439, 10.6425],\n",
       "        [11.2889, 11.3024, 10.7917, 11.4638],\n",
       "        [10.6534, 10.6216, 10.9023, 11.1097]])"
      ]
     },
     "execution_count": 113,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "mat1 + 10.5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 114,
   "metadata": {},
   "outputs": [],
   "source": [
    "# scalar subtraction"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 115,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[ 0.7493, -0.0400,  0.6915,  0.5011],\n",
       "        [ 0.0557, -0.0274,  0.4665,  0.2602],\n",
       "        [ 0.7003,  0.3600, -0.1612,  0.1431],\n",
       "        [ 0.1064,  0.0131,  0.2333,  0.5120],\n",
       "        [-0.0117,  0.2072,  0.0834,  0.6132]])"
      ]
     },
     "execution_count": 115,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "mat2 - 0.20"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 116,
   "metadata": {},
   "outputs": [],
   "source": [
    "# vector and matrix addition"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 117,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[1.6603, 1.4409, 1.8691, 0.3145],\n",
       "        [1.1678, 0.9158, 1.4336, 0.4042],\n",
       "        [1.6743, 1.7145, 1.2814, 1.2255],\n",
       "        [1.0388, 1.0337, 1.3920, 0.8715]])"
      ]
     },
     "execution_count": 117,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "mat1 + vec1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 118,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[1.8348, 1.0722, 1.8812, 0.9629],\n",
       "        [1.1412, 1.0848, 1.6562, 0.7220],\n",
       "        [1.7857, 1.4721, 1.0285, 0.6049],\n",
       "        [1.1918, 1.1252, 1.4230, 0.9738],\n",
       "        [1.0738, 1.3193, 1.2731, 1.0750]])"
      ]
     },
     "execution_count": 118,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "mat2 + vec1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 119,
   "metadata": {},
   "outputs": [],
   "source": [
    "# matrix-matrix addition"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 123,
   "metadata": {},
   "outputs": [
    {
     "ename": "RuntimeError",
     "evalue": "The size of tensor a (4) must match the size of tensor b (5) at non-singleton dimension 0",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mRuntimeError\u001b[0m                              Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-123-bb4bbf5b2f84>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mmat1\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0mmat2\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;31mRuntimeError\u001b[0m: The size of tensor a (4) must match the size of tensor b (5) at non-singleton dimension 0"
     ]
    }
   ],
   "source": [
    "mat1 + mat2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 131,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[0.6004, 0.2795, 0.7733, 0.0028],\n",
       "        [0.0797, 0.0000, 0.1970, 0.0203],\n",
       "        [0.6224, 0.6438, 0.0851, 0.9288],\n",
       "        [0.0235, 0.0148, 0.1618, 0.3717]])"
      ]
     },
     "execution_count": 131,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "mat1 * mat1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# about Bernoulli distribution"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 138,
   "metadata": {},
   "outputs": [],
   "source": [
    "from torch.distributions.bernoulli import Bernoulli"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 139,
   "metadata": {},
   "outputs": [],
   "source": [
    "dist = Bernoulli(torch.tensor([0.3,0.6,0.9]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 140,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([1., 1., 1.])"
      ]
     },
     "execution_count": 140,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dist.sample() #sample is binary, it takes 1 with p and 0 with 1-p"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 132,
   "metadata": {},
   "outputs": [],
   "source": [
    "#Creates a Bernoulli distribution parameterized by probs \n",
    "#Samples are binary (0 or 1). They take the value 1 with probability p \n",
    "#and 0 with probability 1 - p."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 133,
   "metadata": {},
   "outputs": [],
   "source": [
    "from torch.distributions.beta import Beta"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 141,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Beta()"
      ]
     },
     "execution_count": 141,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dist = Beta(torch.tensor([0.5]), torch.tensor([0.5]))\n",
    "dist"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 142,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([0.3067])"
      ]
     },
     "execution_count": 142,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dist.sample()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 143,
   "metadata": {},
   "outputs": [],
   "source": [
    "from torch.distributions.binomial import Binomial"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 144,
   "metadata": {},
   "outputs": [],
   "source": [
    "dist = Binomial(100, torch.tensor([0 , .2, .8, 1]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 147,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([  0.,  29.,  85., 100.])"
      ]
     },
     "execution_count": 147,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dist.sample()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 148,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 100- count of trials\n",
    "# 0, 0.2, 0.8 and 1 are event probabilities"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 174,
   "metadata": {},
   "outputs": [],
   "source": [
    "from torch.distributions.categorical import Categorical"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 175,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Categorical()"
      ]
     },
     "execution_count": 175,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dist = Categorical(torch.tensor([ 0.20, 0.20, 0.20, 0.20, 0.20 ]))\n",
    "dist"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 176,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor(4)"
      ]
     },
     "execution_count": 176,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dist.sample()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 177,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 0.20, 0.20, 0.20, 0.20,0.20 event probabilities"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 155,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Laplace distribution parameterized by loc and ‘scale’."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 157,
   "metadata": {},
   "outputs": [],
   "source": [
    "from torch.distributions.laplace import Laplace"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 161,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Laplace()"
      ]
     },
     "execution_count": 161,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dist = Laplace(torch.tensor([10.0]), torch.tensor([0.990]))\n",
    "dist"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 162,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([10.2167])"
      ]
     },
     "execution_count": 162,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dist.sample()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 163,
   "metadata": {},
   "outputs": [],
   "source": [
    "#Normal (Gaussian) distribution parameterized by loc and ‘scale’."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 165,
   "metadata": {},
   "outputs": [],
   "source": [
    "from torch.distributions.normal import Normal"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 166,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Normal()"
      ]
     },
     "execution_count": 166,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dist = Normal(torch.tensor([100.0]), torch.tensor([10.0]))\n",
    "dist"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 167,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([95.2452])"
      ]
     },
     "execution_count": 167,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dist.sample()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
